package com.key.win.business.chat.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.key.win.business.chat.model.ImApply;
import com.key.win.business.chat.vo.ImApplyVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ImApplyDao extends BaseMapper<ImApply> {
    @Select({"SELECT * FROM ( SELECT a.*, ( SELECT ad.`desc` FROM im_apply_detail ad WHERE ad.apply_Id = a.id ORDER BY ad.create_date DESC LIMIT 0, 1 ) AS `desc`, ( SELECT ad.user_Id FROM im_apply_detail ad WHERE ad.apply_Id = a.id ORDER BY ad.create_date DESC LIMIT 0, 1 ) AS message_user_id, CASE a.type WHEN 0 THEN u.nick_name WHEN 1 THEN su.nick_name ELSE '' END AS `to_user_nick_name`, CASE a.type WHEN 0 THEN u.head_img_url WHEN 1 THEN su.head_img_url ELSE '' END AS `to_user_head_img_url`, CASE a.type WHEN 0 THEN u.id WHEN 1 THEN su.id ELSE '' END AS `to_user_id`, fu.nick_name AS `from_user_nick_name`, fu.head_img_url AS `from_user_head_img_url` FROM im_apply a INNER JOIN sys_user fu ON a.user_id = fu.id LEFT JOIN sys_user u ON a.target_Id = u.id LEFT JOIN im_group g ON g.id = a.target_Id INNER JOIN sys_user su ON g.user_Id = su.id ) im_apply_message ${ew.customSqlSegment}"})
    List<ImApplyVo> findImApply(@Param("ew") Wrapper<ImApplyVo> queryWrapper);
}
